package com.xq.jltx.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.xq.jltx.db.dao.DaoMaster;
import com.xq.jltx.db.dao.DaoSession;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2016/07/19.
 */
public class GreenDaoDBManager {
    private String dbLibName = "greendao-db";
    private DaoSession daoSession;
    private SQLiteDatabase db;
    private DaoMaster.DevOpenHelper helper;
    private DaoMaster daoMaster;
    private GreenDaoDBManager() {
    }

    private static class SingletonHolder{
        static final GreenDaoDBManager INSTANCE = new GreenDaoDBManager();
    }

    public  void initGreenDaoDBManager(Context context,String dbName){
        if(context == null){
            throw new NullPointerException("context == null");
        }
        if(!TextUtils.isEmpty(dbName)){
            dbLibName = dbName;
        }
        helper = new DaoMaster.DevOpenHelper(context,dbLibName,null);
        db = helper.getReadableDatabase();
        daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
    }

    public static GreenDaoDBManager getInstance(){
        return SingletonHolder.INSTANCE;
    }

    public DaoSession getDaoSession() {
        return daoSession;
    }

    public SQLiteDatabase getDb() {
        return db;
    }

    public DaoMaster getDaoMaster() {
        return daoMaster;
    }

}
